/**
 * 
 */
package fr.fermeManager.application.frames;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;

/**
 * @author Mo
 *
 */
public class FrameMessageErreur extends JDialog{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextPane textPane;
	private JLabel lblLabelmessage;
	
	public FrameMessageErreur(String message,Throwable aThrowable) {
		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setMinimumSize(new Dimension(600, 380));
		setPreferredSize(new Dimension(600, 380));
		setTitle("Exception Erreur");
		setIconImage(Toolkit.getDefaultToolkit().getImage(FrameMessageErreur.class.getResource("/img16x16/error.png")));
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setResizable(false);
		getContentPane().setLayout(new MigLayout("", "[grow,center]", "[grow][]"));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Message d'erreur", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		getContentPane().add(panel, "cell 0 0,grow");
		panel.setLayout(new MigLayout("", "[6px,grow]", "[][20px,grow]"));
		
		lblLabelmessage = new JLabel("labelMessage");
		panel.add(lblLabelmessage, "cell 0 0,grow");
		lblLabelmessage.setText(message);
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, "cell 0 1,grow");
		
		textPane = new JTextPane();
		scrollPane.setViewportView(textPane);
		textPane.setBorder(new LineBorder(new Color(0, 0, 0)));
		textPane.setEditable(false);
		textPane.setText(getStackTrace(aThrowable));
		
		JButton btnOk = new JButton("Ok");
		btnOk.setIcon(new ImageIcon(FrameMessageErreur.class.getResource("/img16x16/ok.png")));
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				okMessage();
			}
		});
		getContentPane().add(btnOk, "flowx,cell 0 1");
		
		JButton btnCopierDansLe = new JButton("Copier dans le presse papier");
		btnCopierDansLe.setIcon(new ImageIcon(FrameMessageErreur.class.getResource("/img16x16/copy.png")));
		btnCopierDansLe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				copierDansLePressePapier();
			}
		});
		getContentPane().add(btnCopierDansLe, "cell 0 1");
		setVisible(true);
	}

	protected void okMessage() {
		StringSelection contents = new StringSelection(textPane.getText());
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		clipboard.setContents(contents, null);
		setVisible(false);
		dispose();
		
	}

	protected void copierDansLePressePapier() {
		StringSelection contents = new StringSelection(textPane.getText());
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		clipboard.setContents(contents, null);
	}
	
	 public static String getStackTrace(Throwable aThrowable) {
		    final Writer result = new StringWriter();
		    final PrintWriter printWriter = new PrintWriter(result);
		    aThrowable.printStackTrace(printWriter);
		    return result.toString();
		  }
}
